package xia.wan.glowingterms.service.impl;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import xia.wan.glowingterms.mapper.EmailMapper;
import xia.wan.glowingterms.pojo.CrontabEmail;
import xia.wan.glowingterms.service.SendMailservice;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;

/**
 * @PACKAGE_NAME: xia.wan.glowingterms.service
 * @AUTHOR: 太白
 * @DATE: 2022/12/01/19:05
 **/
@Service
public class SendMail implements SendMailservice {



    @Resource
    private JavaMailSender mailSender;

    @Resource
    private EmailMapper emailMapper;

    @Value("${spring.mail.username}")
    private String from;

    @Value("${she.mail}")
    private String[] sheMail;

    @Value("${api.glow}")
    private String call;
    @Value("${api.weather}")
    private String weather;

    /**
     * 发送邮件的方法
     * @param subject   邮件标题
     * @param message   邮件内容
     */
    private void sendMessage(String subject,String message) {
        try {
            MimeMessage mimeMessage = mailSender.createMimeMessage();
            MimeMessageHelper helper = new MimeMessageHelper(mimeMessage);
            helper.setFrom(from);       //发送方邮件名
            helper.setTo(sheMail);         //接收方邮件地址
/*            helper.setTo(she1Mail);
            helper.setTo(she2Mail);*/
            helper.setSubject(subject);     //邮件标题
            helper.setText(message,true);   //邮件内容，是否为html格式
            mailSender.send(helper.getMimeMessage());
        } catch (MessagingException e) {
            System.out.println(e);
        }
    }


    /**
     * 调用api获取小情话
     * @return  需要发送的消息
     */
    private String getLovePrattle() {
        String result1= HttpUtil.get(call);
        System.out.println(result1);
        return result1;
    }

    /**
     * 调用api获取天气，可以通过创建     和风天气  网站创建账号弄到api
     * @return 需要发送的消息
     */
    private String getWeather() {
        StringBuffer sb = new StringBuffer();
        String s = HttpUtil.get(weather);
        JSONObject jsonObject = JSONUtil.parseObj(s);
        System.out.println("时间为："+jsonObject.get("updateTime"));
        sb.append("<p>时间为："+jsonObject.get("updateTime")+"<br>\n");
        System.out.println("详细天气请查看"+jsonObject.get("fxLink"));
        sb.append("详细天气请查看"+jsonObject.get("fxLink")+"<br>\n");
        Object now = jsonObject.get("now");
        JSONObject nowJson = JSONUtil.parseObj(now);
        System.out.println("当前温度为"+nowJson.get("temp")+"℃");
        sb.append("当前温度为"+nowJson.get("temp")+"℃"+"<br>\n");
        System.out.println("天气状况为"+nowJson.get("text"));
        sb.append("天气状况为"+nowJson.get("text")+"<br>\n");
        System.out.println("风力等级"+nowJson.get("windScale")+"级");
        sb.append("风力等级"+nowJson.get("windScale")+"级"+"<br>\n");
        System.out.println("能见度"+nowJson.get("vis")+"公里");
        sb.append("能见度"+nowJson.get("vis")+"公里</p>");
        String weater = sb.toString();
        return weater;
    }


    @Override
    public void sendLovePrattle() {
        String lovePrattle = this.getLovePrattle();
        String massageHeader = "来自Xsqone的今日小情话";
        this.sendMessage(massageHeader,lovePrattle);
        CrontabEmail crontabEmail = new CrontabEmail();
        crontabEmail.setSender(from);
        StringBuffer stringBuffer = new StringBuffer();
        for (String mail : sheMail) {
            stringBuffer.append(mail+",");
        }
//        System.out.println(stringBuffer.toString());
        crontabEmail.setRecipient(stringBuffer.toString());
        crontabEmail.setMassageHeader(massageHeader);
        crontabEmail.setContent(lovePrattle);
        emailMapper.insert(crontabEmail);

    }

    @Override
    public void sendWeather() {
        String weather = this.getWeather();
        String massageHeader = "来自Xsqone的今日天气关怀";
        this.sendMessage(massageHeader,weather);
        CrontabEmail crontabEmail = new CrontabEmail();
        crontabEmail.setSender(from);
        crontabEmail.setRecipient(sheMail.toString());
        crontabEmail.setMassageHeader(massageHeader);
        crontabEmail.setContent(weather);
        emailMapper.insert(crontabEmail);

    }
}
